import React, { useState } from 'react';
import { Modal, Form, Row, Col, Input } from 'antd';
import { FormInstance } from 'antd/es/form/Form';
import ASelect, { ASelectOptionType } from '@/components/SelfComp/ASelect';
import { AppItem } from '@/types/app';
import { DataItem, FormModalProps } from '@/types/global';
import { TradeItem } from '@/types/trade';
import { formLayout, modalFormItemLayout, maxModalFormItemLayout } from '@/utils/commons';

type AddTradeProps<T> = FormModalProps<T> & {
  appList: AppItem[];
  systemData: DataItem[];
  clusterData: DataItem[];
}

type AddTradePropsEx<T> = Omit<AddTradeProps<T>, 'formData'>;

const FormItem = Form.Item;

const AddTradeModal: React.FC<AddTradePropsEx<TradeItem>>  = (props) => {

  const [appData, setAppData] = useState<DataItem[]>([]);
  const [systemCode, setSystemCode] = useState<string>('');
  const formRef = React.createRef<FormInstance>();
  const [form] = Form.useForm<TradeItem>();

  const onOk = () => {
    form.validateFields().then(fieldsValue => {
      console.info(fieldsValue);
      const values = {
        ...fieldsValue
      };
      console.log('Received values of form: ', values);
      const { onHandlerOK } = props;
      if (onHandlerOK) {
        onHandlerOK({
          ...values
        });
      }
    }).catch();
  };

  const onCancel = () => {
    // 先清除form表单
    form.resetFields();
    const { onHandlerCancel } = props;
    if (onHandlerCancel) {
      onHandlerCancel();
    }
  }

  /**
   * 交易所属系统栏位改变事件
   * 默认情况下 onChange 里只能拿到 value
   */
  const systemCodeChange = (value: string, option: ASelectOptionType | ASelectOptionType[]) => {
    form.resetFields([
      'clstCode',
      'appCode'
    ]);
    // 其实可以筛选集群
    // 但这里暂时不做此功能
    setSystemCode(value);
  }

  /**
   * 交易所属集群栏位改变事件
   * 默认情况下 onChange 里只能拿到 value
   */
  const clstCodeChange = (value: string, option: ASelectOptionType | ASelectOptionType[]) => {
    form.resetFields([
      'appCode'
    ]);
    // 根据全量的系统应用列表，所选的系统，集群，筛选应用列表
    const clstCode = value;
    const { appList } = props;
    if (appList && appList.length > 0) {
      const appData: DataItem[] = [];
      appList.forEach(item => {
        const srcSystemCode = item.systemCode;
        const srcClstCode = item.cluster;
        if (systemCode === srcSystemCode && clstCode === srcClstCode) {
          const dataItem: DataItem = {
            key: item.appCode,
            value: item.appName
          }
          appData.push(dataItem);
        }
      });
      setAppData(appData);
    }
  }

  const { colon, modalTitle, modalWidth, modalVisible, systemData, clusterData, loading } = props;
  const formItemLayout = modalFormItemLayout;
  // 是否有表格勾选数据传进来，保证传进的值不为undefined
  // console.info(formData);

  return (
    <Modal
      title={modalTitle}
      destroyOnClose
      maskClosable={false}
      width={modalWidth}
      open={modalVisible}
      confirmLoading={loading}
      onOk={onOk}
      onCancel={onCancel}>
      <Form layout={formLayout} ref={formRef} form={form}>
        <Row>
          <Col span={12}>
            <FormItem label='交易功能码' name='funcCode' {...formItemLayout} colon={colon}
              rules={[
                { max: 20, message: '交易功能码最多允许输入20个字符' },
                { required: true, message: '交易功能码必输' }
              ]}
            >
              <Input/>
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label='交易名称' name='funcName' {...formItemLayout} colon={colon}
              rules={[
                { max: 30, message: '交易名称最多允许输入30个字符' },
                { required: true, message: '交易名称必输' }
              ]}
            >
              <Input/>
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <FormItem label='交易路径' name='funcAction' {...maxModalFormItemLayout} colon={colon}
              rules={[
                { max: 100, message: '交易路径最多允许输入100个字符' },
                { required: true, message: '交易路径必输' },
              ]}
            >
              <Input/>
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label='负责人' name='owner' {...formItemLayout} colon={colon}
              rules={[
                { max: 20, message: '负责人最多允许输入20个字符' },
                { required: true, message: '负责人必输' }
              ]}
            >
              <Input/>
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label='交易所属系统' name='systemCode' {...formItemLayout} colon={colon}
              rules={[
                { required: true, message: '交易所属系统必输' }
              ]}
            >
              <ASelect dataSource={systemData} onChange={systemCodeChange}/>
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label='交易所属集群' name='clstCode' {...formItemLayout} colon={colon}
              rules={[
                { required: true, message: '交易所属集群必输' }
              ]}
            >
              <ASelect dataSource={clusterData} onChange={clstCodeChange}/>
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label='交易所属应用' name='appCode' {...formItemLayout} colon={colon}
              rules={[
                { required: true, message: '交易所属应用必输' }
              ]}
            >
              <ASelect dataSource={appData}/>
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Modal>
  );
}

export default AddTradeModal;